package view;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.TreeMap;

import javax.imageio.ImageIO;

public class ImageCache {
	TreeMap<String,BufferedImage> map;
	BufferedImage notFoundImage;
	ImageCache(){
		 map = new TreeMap<String,BufferedImage>();
		 try{
			 File file = new File("images\\404.png");
			 if(file.exists()){
				 notFoundImage = ImageIO.read(file);
			 }
		 }
		 catch(IOException e){
			 System.out.println("\n\n\n\n\n\n\n\n\nAWWWWWWWWWWWA\\n\n\n\n\n\n\n\n\n\n\n\n\n\ns");
			 notFoundImage = new BufferedImage(40, 60, BufferedImage.TYPE_4BYTE_ABGR);
		 }
	}
	public BufferedImage getImage(String str){
		if( !map.containsKey(str))
			addImage(str);
		BufferedImage img = map.get(str);
		if(img == null)
			System.out.println(" IMG IS NUL!!");
		return img;
	}
	private void addImage(String str){
		try{
			File file = new File("images\\"+str+".png");
			 if(file.exists()){
				 map.put(str, ImageIO.read(file));
			 }
			 else{
				 map.put(str,notFoundImage);
			 }
		 }
		 catch(IOException e){
			 map.put(str,notFoundImage);
		 }
	}
	public BufferedImage getNotFoundImage(){
		return notFoundImage;
	}

}
